package com.customization.assets.util;

import com.customization.commons.LogTool;
import weaver.hrm.User;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *  用于资产登记时下载mac的小程序
 */
public class DownLoadMacTool extends HttpServlet {
    private LogTool log = new LogTool("/log/assetlog/huangwm/DownLoadMacTool",false);
    private static DateTools dateTools = new DateTools();
    public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String fileNmae = "运行获取Mac信息.exe";
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-disposition", "attachment;filename="+new String(fileNmae.getBytes(),"ISO8859-1"));
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("weaver_user@bean");
        String userid = user.getUID()+"";
        log.writeLog("用户id="+userid+"  用户名="+user.getLastname()+" 在"+dateTools.getCurrentTimes()+"下载了该文件");
        ServletOutputStream os = response.getOutputStream();
        InputStream is = new FileInputStream("D:\\WEAVER\\ecology\\ecustom\\运行获取mac.exe");
        byte[] buff = new byte[2048];
        int size = 0;
        while((size = is.read(buff))!=-1){
            os.write(buff,0,size);
        };
        is.close();
        os.flush();
        os.close();
    }
}
